<template>
  <div>
    <table-box
      class="page-table"
      :data="formBizChallengeModule.module.impl.dataList"
      :size="defaultFormItemSize"
      @sort-change="formBizChallengeModule.module.impl.onSortChange"
      @refresh="refreshFormChallengeModule(true)"
      :seq-config="{
        startIndex:
          (formBizChallengeModule.module.impl.currentPage - 1) *
          formBizChallengeModule.module.impl.pageSize
      }"
    >
      <el-button
        slot="operator"
        type="primary"
        icon="el-icon-plus"
        :size="defaultFormItemSize"
        @click="onAddRow()"
        >新建</el-button
      >

      <vxe-column title="序号" type="seq" width="50px" />
      <vxe-column title="模块名称" field="name" />
      <vxe-column title="模块图片" field="imgUrl" width="150px">
        <template slot-scope="scope">
          <img :src="scope.row.fullFilePath" width="100" height="100" />
        </template>
      </vxe-column>
      <vxe-column title="顺序" field="sortNum" />
      <vxe-column title="是否启用">
        <template slot-scope="scope">
          <span>{{ scope.row.showAble ? '启用' : '禁用' }}</span>
        </template>
      </vxe-column>
      <vxe-column title="创建时间">
        <template slot-scope="scope">
          <span>{{ formatDateByStatsType(scope.row.createTime, 'day') }}</span>
        </template>
      </vxe-column>
      <vxe-column title="操作" fixed="right" width="230px">
        <template slot-scope="scope">
          <el-button
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="onEditRow(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            class="btn-table-danger"
            type="text"
            :size="defaultFormItemSize"
            @click="onDeleteRow(scope.row)"
          >
            删除
          </el-button>
        </template>
      </vxe-column>

      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px">
        <el-pagination
          :total="formBizChallengeModule.module.impl.totalCount"
          :current-page="formBizChallengeModule.module.impl.currentPage"
          :page-size="formBizChallengeModule.module.impl.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, prev, pager, next, sizes"
          @current-change="formBizChallengeModule.module.impl.onCurrentPageChange"
          @size-change="formBizChallengeModule.module.impl.onPageSizeChange"
        >
        </el-pagination>
      </el-row>
    </table-box>
  </div>
</template>

<script>
import { treeDataTranslate } from '@/utils';
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin, cachedPageChildMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import { BizChallengeModuleController } from '@/api';
import formEditBizChallengeModule from '@/views/biz/formEditBizChallengeModule';

export default {
  name: 'formsBizChallengeModule',
  props: {},
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      deptIdPath: [],
      formBizChallengeModule: {
        module: {
          impl: new TableWidget(
            this.loadBizChallengeModule,
            this.loadBizChallengeModuleVerify,
            true,
            false,
            'createTime',
            1
          )
        },
        isInit: false
      }
    };
  },
  methods: {
    onAddRow() {
      this.$dialog
        .show('新建模块', formEditBizChallengeModule, {
          area: '1200px'
        })
        .then(res => {
          this.refreshFormChallengeModule();
        })
        .catch(() => {});
    },
    onEditRow(row) {
      this.loadRowData(row)
        .then(rowData => {
          this.$dialog
            .show(
              '编辑模块',
              formEditBizChallengeModule,
              {
                area: '1200px'
              },
              {
                rowData: rowData
              }
            )
            .then(res => {
              this.refreshFormChallengeModule();
            })
            .catch(e => {});
        })
        .catch(e => {
          //
        });
    },
    onDeleteRow(row) {
      let params = {
        id: row.id
      };
      BizChallengeModuleController.delete(this, params)
        .then(res => {
          if (res.success) {
            this.$message.success('删除成功');
            this.refreshFormChallengeModule();
          } else {
            this.$message.error('删除失败');
          }
        })
        .catch(e => {
          console.err(e);
        });
    },
    loadRowData(row) {
      return new Promise((resolve, reject) => {
        let params = {
          id: row.id
        };
        BizChallengeModuleController.findById(this, params)
          .then(res => {
            resolve(res.data);
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizChallengeModule(params) {
      return new Promise((resolve, reject) => {
        BizChallengeModuleController.page(this, {})
          .then(res => {
            resolve({
              dataList: res.data.dataList,
              totalCount: res.data.totalCount
            });
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizChallengeModuleVerify() {
      return true;
    },
    refreshFormChallengeModule(reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formBizChallengeModule.module.impl.refreshTable(true, 1);
      } else {
        this.formBizChallengeModule.module.impl.refreshTable();
      }
      this.formBizChallengeModule.isInit = true;
    },
    onResume() {
      this.refreshFormChallengeModule();
    },
    initFormData() {},
    formInit() {
      this.initFormData();
      this.refreshFormChallengeModule();
    }
  },
  created() {
    this.formInit();
  }
};
</script>
